package com.example.yk2021xm04.controller;


import com.example.yk2021xm04.model.RespBean;
import com.example.yk2021xm04.pojo.Notice;
import com.example.yk2021xm04.query.NoticeQuery;
import com.example.yk2021xm04.service.INoticeService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Resource
    private INoticeService noticeService;

    /**
     * 通知管理主界面
     * @return
     */
    @RequestMapping("index")
    public String index() {
        return "notice/notice";
    }

    /**
     * 添加更新通知页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("addOrUpdateNoticePage")
    public String addOrUpdatePage(Integer id, Model model) {
        if (null!=id) {
            model.addAttribute("notice",noticeService.getById(id));
        }
        return "notice/add_update";
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> noticeList(NoticeQuery noticeQuery) {
        return noticeService.noticeList(noticeQuery);
    }

    @RequestMapping("delete")
    @ResponseBody
    public RespBean deleteNotice(Integer[] ids) {
        noticeService.deleteNotice(ids);
        return RespBean.success("通知记录删除成功");
    }

    @RequestMapping("add")
    @ResponseBody
    public RespBean addNotice(Notice notice) {
        noticeService.addNotice(notice);
        return RespBean.success("通知记录添加成功");
    }


    @RequestMapping("update")
    @ResponseBody
    public RespBean updateNotice(Notice notice) {
        noticeService.updateNotice(notice);
        return RespBean.success("通知记录更新成功");
    }

    @RequestMapping("count")
    public String statistic() {return "notice/notice-statistics";}

    @RequestMapping("statistic")
    @ResponseBody
    public RespBean noticeStatistic() {
        //发布时间分组 group by day
        List<Object> announceTimeByDay = noticeService.anTimeByDay();
        //一天重发布的通知数
        List<Object> announceCountByDay = noticeService.anCountByDay();
        //日同比
        double rate =0.0;
        //总通知数
        Integer totalNoticeCount = noticeService.count();
        //今天发布的任务数量
        Integer NoticeCountInDay =noticeService.NoticeCountInDay();
        //昨天发布的任务数量
        Integer NoticeCountInYesterday = noticeService.NoticeCountInYesterday();

        rate= (double)(NoticeCountInDay - NoticeCountInYesterday) / NoticeCountInYesterday;
        if(NoticeCountInYesterday==0){
            rate = 0;
        }
        Map<String,Object> res = new HashMap<>();
        res.put("num",announceCountByDay);
        res.put("time",announceTimeByDay);
        res.put("total",totalNoticeCount);
        res.put("dayCount",NoticeCountInDay);
        res.put("rate",rate);
        return RespBean.success("任务统计数据返回成功",res);
    }
}
